import { i18n } from "@/locale";
const useI18n: any = i18n.global;
const t = useI18n.t;

//手机号正则
function validatorMobile(mobile: string): boolean {
  const reg = /^[1][2,3,4,5,6,7,8,9][0-9]{9}$/;
  if (reg.test(mobile)) {
    return true;
  } else {
    return false;
  }
}
//密码正则，数字，字母，特殊符号至少两种，长度6到20
function validatorPassword(password: string): boolean {
  const reg = /(?!\d+$)(?!^[a-zA-Z]+$)(?!^[_#@]+$).{6,20}/;
  if (reg.test(password)) {
    return true;
  } else {
    return false;
  }
}
//element  手机号校验回调
const validateMobile = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error(t("validate.mobile.empty")));
  } else {
    if (!validatorMobile(value)) {
      callback(new Error(t("validate.mobile.error")));
    } else {
      callback();
    }
  }
};
//element  密码校验回调
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error(t("validate.password.empty")));
  } else {
    if (!validatorPassword(value)) {
      callback(new Error(t("validate.password.error")));
    } else {
      callback();
    }
  }
};
//element  用户名校验回调
const validateuserName = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error(t("validate.userName.empty")));
  } else {
    //中文数字字母，长度1到10
    const reg = /^[A-Za-z0-9\u4e00-\u9fa5]{1,10}$/gi;
    if (!reg.test(value)) {
      callback(new Error(t("validate.userName.error")));
    } else {
      callback();
    }
  }
};
//element  邮箱校验回调
const validateEmail = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback();
  } else {
    const reg =
      /^[A-Za-zd0-9]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/;
    if (!reg.test(value)) {
      callback(new Error(t("validate.email.error")));
    } else {
      callback();
    }
  }
};
// 6位数字验证码
const validateCode = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error(t("validate.code.empty")));
  } else {
    const reg = /^\d{6}$/;
    if (!reg.test(value)) {
      callback(new Error(t("validate.code.error")));
    } else {
      callback();
    }
  }
};
//密码不为空的简单验证
const validatePwd = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error(t("validate.password.empty")));
  } else {
    callback();
  }
};

export {
  validatorMobile,
  validatorPassword,
  validatePwd,
  validateMobile,
  validatePassword,
  validateuserName,
  validateEmail,
  validateCode,
};
